package com.example.back.mapper;

import com.example.back.dto.req.DoctorListReq;
import com.example.back.dto.resp.DepList;
import com.example.back.dto.resp.PharmacisRsp;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.SelectProvider;
import org.apache.ibatis.jdbc.SQL;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Component;
import org.springframework.util.ObjectUtils;

import java.util.List;

@Component
public interface PharmacistMapper {

    @SelectProvider(type = PharmacistMapper.CommonProvider.class, method = "List")
    List<PharmacisRsp> List(@Param("doctorListReq") DoctorListReq doctorListReq);

    @Select("select a.id_bigint AS ID,a.name_nvarchar AS Name from h_hospital_department a where Disable_bit = 0 and not exists " +
            "(select 1 from h_hospital_department b where b.pd_id_bigint = a.id_bigint)")
    List<DepList> findAllH();

    class CommonProvider {
        public String List(DoctorListReq doctorListReq) {
            SQL sql = new SQL();
            sql.SELECT("a.dc_id_bigint AS DCID, a.name_nvarchar AS Name, c.name_nvarchar AS DepName,a.title_nvarchar AS Title,a.phone_nvarchar AS Phone,a.disable_bit " +
                    "AS Disable FROM doctor_pharmacist a INNER JOIN h_hospital_department c ON(a.depart_id_bigint=c.id_bigint) ");
            if (!ObjectUtils.isEmpty(doctorListReq.getName())) {
                String name = doctorListReq.getName();
                sql.WHERE("a.name_nvarchar LIKE CONCAT ('%',#{name},'%')");
            }
            sql.ORDER_BY("a.code_time_datetime DESC");
            return sql.toString();
        }
    }
}
